package org.intellij.doctree;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ProjectDocsWorkspace implements ProjectComponent, JDOMExternalizable {

    public static final String COMPONENT_NAME = "org.intellij.doctree.ProjectDocsWorkspace";

    private List<String> projExpRowList = new ArrayList<String>();
    private List<String> appExpRowList = new ArrayList<String>();

    private Project project;

    public ProjectDocsWorkspace(Project project) {
        this.project = project;
    }

    public void projectOpened() {
        //Do nothing
    }

    public void projectClosed() {
        project = null;
    }

    @NotNull
    @NonNls
    public String getComponentName() {
        return COMPONENT_NAME;
    }

    public void initComponent() {
        //Do nothing
    }

    public void disposeComponent() {
        //Do nothing
    }

    public void readExternal(Element element) throws InvalidDataException {
        appExpRowList = XMLHelper.readExpandedNodes(element, "Application");
        projExpRowList = XMLHelper.readExpandedNodes(element, "Project");
    }

    public void writeExternal(Element element) throws WriteExternalException {
        ProjectDocs docsComp = (ProjectDocs) project.getComponent(ProjectDocs.COMPONENT_NAME);
        appExpRowList = docsComp.getAppTreeExpandedState();
        projExpRowList = docsComp.getProjTreeExpandedState();
        XMLHelper.writeExpandedNodes(element, "Application", appExpRowList);
        XMLHelper.writeExpandedNodes(element, "Project", projExpRowList);
    }

    public List<String> getProjExpRowList() {
        return projExpRowList;
    }

    public List<String> getAppExpRowList() {
        return appExpRowList;
    }

    public void setProjExpRowList(List<String> rowList) {
        projExpRowList = rowList;
    }

    public void setAppExpRowList(List<String> rowList) {
        appExpRowList = rowList;
    }

}
